<?php
class AccountData{

    protected $db;
    protected $accountsId = array();
    static protected $callData = null;
    static protected $meetingData = null;

    public function  __construct() {
        $this->db = $GLOBALS['db'];
    }

    public function setAccounts(array $accountsId){
        $this->accountsId = $accountsId;
    }
    
    protected function getLastData($accountId){
            if(!is_array(self::$callData)){
                self::$callData = $this->getCallData();
            }

            if(!is_array(self::$meetingData)){
                self::$meetingData = $this->getMeetingData();
            }

            $lastData = '';
            if(isset(self::$callData[$accountId])){
                $lastData = self::$callData[$accountId];
                if(isset(self::$meetingData[$accountId])){
                    if(self::$callData[$accountId]['dateEntered'] > self::$meetingData[$accountId]['dateEntered']){
                        $lastData = self::$callData[$accountId];
                    }else{
                        $lastData = self::$meetingData[$accountId];
                    }
                }
            }elseif(isset(self::$meetingData[$accountId])){
                $lastData = self::$meetingData[$accountId];
            }

            return $lastData;
        }

        public function getLastTopic($accountId){
            $lastData = $this->getLastData($accountId);

            return is_array($lastData)? $lastData['topic'] : $lastData;
        }

        public function getContactData($accountId){
            $lastData = $this->getLastData($accountId);

            $contactData = "";
            if(is_array($lastData)){
                $contactData = $lastData['email'];
                if(isset($lastData['phoneWork'])){
                    $contactData .= '<br/>'.$lastData['phoneWork'];
                }
                if(isset($lastData['phoneMobile'])){
                    $contactData .= '<br/>'.$lastData['phoneMobile'];
                }
            }

            return $contactData;
        }

        protected function getCallData(){
            $callSql = "SELECT c.name, c.date_entered, ea.email_address as email, con.first_name, con.last_name,
                        con.phone_mobile as phoneMobile, con.phone_work as phoneWork, a.id as accountId
                FROM `calls` c
                INNER JOIN accounts_contacts ac ON c.parent_id = ac.contact_id
                inner join contacts con on con.id = ac.contact_id
                INNER JOIN accounts a ON a.id = ac.account_id
                LEFT JOIN email_addr_bean_rel eabr ON con.id = eabr.bean_id AND bean_module='Contacts'
                LEFT JOIN email_addresses ea ON ea.id=eabr.email_address_id
                WHERE c.date_entered =
                (
                    SELECT max(c1.date_entered) FROM calls c1
                    INNER JOIN accounts_contacts ac1 ON c1.parent_id = ac1.contact_id
                    WHERE ac1.account_id = a.id
                )
                AND c.deleted = 0
                AND a.deleted = 0
                AND ac.deleted = 0";

            if(!empty($accountsId)){
                $callSql .= " AND a.id IN(\"'".implode('","', $accountsId)."\") ";
            }
            $callSql .= " GROUP BY a.id";

            return $this->createActivityData($callSql);

        }

        protected function getMeetingData(){
            $meetingSql = "SELECT m.name, m.date_entered, ea.email_address as email, con.first_name, con.last_name,
                con.phone_mobile as phoneMobile, con.phone_work as phoneWork, a.id as accountId
                FROM `meetings` m
                INNER JOIN accounts_contacts ac ON m.parent_id = ac.contact_id
                inner join contacts con on con.id = ac.contact_id
                INNER JOIN accounts a ON a.id = ac.account_id
                LEFT JOIN email_addr_bean_rel eabr ON con.id = eabr.bean_id AND bean_module='Contacts'
                LEFT JOIN email_addresses ea ON ea.id=eabr.email_address_id
                WHERE m.date_entered =
                (
                    SELECT max(m1.date_entered) FROM meetings m1
                    INNER JOIN accounts_contacts ac1 ON m1.parent_id = ac1.contact_id
                    WHERE ac1.account_id = a.id
                )
                AND m.deleted=0
                AND a.deleted = 0
                AND ac.deleted = 0";

            if(!empty($accountsId)){
                $meetingSql .= " AND a.id IN(\"'".implode('","', $accountsId)."\") ";
            }
            $meetingSql .= " GROUP BY a.id";

            return $this->createActivityData($meetingSql);

        }

        private function createActivityData($sql){
            $data = array();

            $result = $this->db->query($sql);
            while($row = $this->db->fetchByAssoc($result)) {
                $data[$row['accountId']] = array(
                    'dateEntered' => $row['date_entered'],
                    'topic' => $row['name'],
                    'email' => $row['email'],
                    'phoneMobile' => $row['phoneMobile'],
                    'phoneWork' => $row['phoneWork'],
                    );
            }

            return $data;
        }
}